using MoonFramework.Scripts.View.UI;
using MoonFramework.Template;
using UnityEngine;
using UnityEngine.UI;

namespace UI.MenuUI
{
    [UIElement(false, "UI/UI_StartGameWindow", 0)]
    public class UI_StartGameWindow : BaseUIWindow
    {
        [SerializeField] private Button backButton;
        [SerializeField] private Button StartGameButton;
        [SerializeField] private Slider mapSize_Slider;
        [SerializeField] private InputField mapSeed_InputField;
        [SerializeField] private InputField spawn_InputField;
        [SerializeField] private Slider marshLimit_Slider;

        public override void Init()
        {
            base.Init();
            backButton.onClick.AddListener(Close);
            StartGameButton.onClick.AddListener(StartGame);

            backButton.BindMouseEffect();
            StartGameButton.BindMouseEffect();
        }

        public override void Close()
        {
            base.Close();
            backButton.RemoveMouseEffect();
            StartGameButton.RemoveMouseEffect();
            UIManager.Instance.Show<UI_MainMenuWindow>();
        }

        private void StartGame()
        {
            var mapSize = (int)mapSize_Slider.value;
            //如果玩家不输入种子的值，则随机
            var mapSeed = string.IsNullOrEmpty(mapSeed_InputField.text)
                ? Random.Range(int.MinValue, int.MaxValue)
                : int.Parse(mapSeed_InputField.text);

            var spawnSeed = string.IsNullOrEmpty(spawn_InputField.text)
                ? Random.Range(int.MinValue, int.MaxValue)
                : int.Parse(spawn_InputField.text);
            var marshLimit = marshLimit_Slider.value;
            UIManager.Instance.CloseAll();
            //建立新的存档，并且开始游戏
            GameManger.Instance.CreateNewArchive_EntryGame(mapSize, mapSeed, spawnSeed, marshLimit);
        }
    }
}